<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//详细页控制器
class Detailcontroller extends Controller{
	//详细页
	public function index(){
		$goods_gid = Q('get.goods_gid',0,'intval');
		//p($goods_gid);exit;
		if($goods_gid){
			//商品
			$goodsModel = new \Common\Model\Goods;
			$goodsData = $goodsModel->where("gid",$goods_gid)->find();
			//分配数据
			View::with('goodsData',$goodsData);
			//p($goodsData);
			
			//根据taid=typeattr_taid来获得数据  
			//先获取class=1的数据，再往里面压入一个字段，保存所有规格
			//这样页面循环一下就行，页面不需再做判断
			$goodsAttr = Db::table('goods g')->join('typeattr ta','g.type_tid','=','ta.type_tid')->where('gid',$goods_gid)->where('class',1)->get();   // ****没起作用
			//p($goodsAttr);
			foreach ($goodsAttr as $k => $v) {
				$spec=Db::table('goods_attr')->where('goods_gid',$goods_gid)->where('typeattr_taid',$v['taid'])->get();
				//p($spec);
				$goodsAttr[$k]['spec']=$spec;
//				$goodsAttr[$k]['spec'] = Db::table('goods_attr')->where('goods_gid',$goods_gid)->where('typeattr_taid',$v['taid'])->get();
			}
			//die;
			//p($goodsAttr);
			
			//分配数据
			View::with('goodsAttr',$goodsAttr);

					
			//详细		
			$detailModel = new \Common\Model\Detail;
			$detailData = $detailModel->where("goods_gid",$goods_gid)->find();				
			$detailData['detail_pic'] = explode('|', $detailData['detail_pic']);
			//p($detailData);
			//分配数据
			View::with('detailData',$detailData);
			
			
			//货品列表
			$listModel = new \Common\Model\GoodsList;
			$listData = $listModel->where("goods_gid",$goods_gid)->where('is_default',1)->find();				
			$listData['combine'] = explode('|', $listData['combine']);
			
			//分配数据
			View::with('listData',$listData);
			
			
			
			//判断货品列表默认  //没有货品，前台会报错 undefined offset:1  加了货品，前台就好了
			$listAllModel = new \Common\Model\GoodsList;
			$listAllData = $listAllModel->where("goods_gid",$goods_gid)->get();	
			foreach($listAllData as $k=>$v){
				$listAllData[$k]['combine'] = explode('|', $listAllData[$k]['combine']);
				
				if($listData['combine'][0] == $listAllData[$k]['combine'][0]){
					$listData['combine'][] = $listAllData[$k]['combine'][1];
				}
				 
			}			
			//p($listData);
			//p($listAllData);

			//分配数据
			View::with('listAllData',$listAllData);

			
		}
		//载入模板
		View::make();
	}
			
} 
	
?>